/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package cn.piaoyi.dao.hibernate;


import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.piaoyi.bean.Category;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.dao.CategoryDAO;

/**
 * 功能描述:类别操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class CategoryHibernateDAO extends HibernateDaoSupport implements CategoryDAO {

	private static final String LOAD_BY_ID = "from Category where id = ?";
	private static final String LOAD_BY_CODE = "from Category where code = ?";
	private static final String  REMOVE_BY_ID = "delete from Category where id = ?";
	
	/**
	 * 保存Category对象
	 * 
	 * @param category
	 * 			Category对象
	 * @return Category
	 * 			Category对象
	 */
	public Category saveCategory(Category category){
		this.getHibernateTemplate().save(category);
		return category;
	}
	/**
	 * 保存或更新Category对象
	 * 
	 * @param category
	 * 			Category对象
	 * @return Category
	 * 			Category对象
	 */
	public Category saveOrUpdateCategory(Category category){
		this.getHibernateTemplate().saveOrUpdate(category);
		return category;
	}
	/**
	 * 更新Category对象
	 * 
	 * @param category
	 * 			Category对象
	 * @return Category
	 * 			Category对象
	 */
	public Category updateCategory(Category category){
		this.getHibernateTemplate().update(category);
		return category;
	}
	
	/**
	 * 根据主键ID获取Category对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Category
	 * 			Category对象
	 */
	public Category findCategoryById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Category) l.get(0);
		}
	}
	
	/**
	 * 根据code获取Category对象
	 * 
	 * @param code
	 * 			String
	 * @return Category
	 * 			Category对象
	 */
	public Category findCategoryByCode(String code){
		List l = this.getHibernateTemplate().find(LOAD_BY_CODE,code);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Category) l.get(0);
		}
	}
	
	/**
	 * 获取Category个数
	 * 
	 * @return int
	 * 			Category个数
	 */
	public long getCategoryNum(){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Category.class);
				c.setProjection(Projections.count("id"));
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Integer) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取Category列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Category对象列表
	 */
	public List findCategorys(final String orderby,final int ascOrDesc,final int firstResult,final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Category.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
	
	/**
	 * 获取Category列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Admin对象列表
	 */
	public List findCategorys(final String orderby, final int ascOrDesc){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Category.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				return c.list();
			}
		});
	}
	
	/**
	 * 删除Category对象
	 */
	public void removeCategoryById(final long id){
	    getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
}
